package com.jiayang.hhotel.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.InputType;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.jiayang.hhotel.R;
import com.jiayang.hhotel.bean.Constant;
import com.jiayang.hhotel.bean.InfoModel;
import com.jiayang.hhotel.presenter.PRegister;
import com.jiayang.hhotel.util.StringUtil;
import com.jiayang.hhotel.util.ToastUtil;

import cn.droidlover.xdroidmvp.mvp.XActivity;
import cn.droidlover.xdroidmvp.net.NetError;

/**
 * Created by Administrator on 2017/5/16 0016.
 * 注册
 *
 */

   public class RegistActivity extends XActivity<PRegister>  implements View.OnClickListener{
    private EditText mobile_num_et;//输入手机号
    private EditText password_et;//输入密码
    private EditText iden_code_et;//输入验证码
    private TextView getiden_code_tv;//获取验证码
    private TextView register;//注册
    private ImageView see_password_iv;//密码可见
    private String code;//验证码
    private boolean seepass=false;
    private String vphonenum="";
    private CountDownTimer timer = new CountDownTimer(60000, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {

            getiden_code_tv.setText((millisUntilFinished / 1000) + "秒后可重发");
        }

        @Override
        public void onFinish() {
            getiden_code_tv.setEnabled(true);
            getiden_code_tv.setBackgroundResource(R.mipmap.yujiaojuxingh);
            getiden_code_tv.setText(getString(R.string.get_verification_code));
        }
    };

    @Override
    public void initData(Bundle savedInstanceState) {
        initView();

    }

    //初始化view;
    private void initView(){
        mobile_num_et=(EditText)findViewById(R.id.mobile_num_et);
        password_et=(EditText)findViewById(R.id.password_et);
        iden_code_et=(EditText)findViewById(R.id.iden_code_et);
        getiden_code_tv=(TextView) findViewById(R.id.getiden_code_tv);
        getiden_code_tv.setOnClickListener(this);
        register=(TextView) findViewById(R.id.register);
        register.setOnClickListener(this);
        see_password_iv=(ImageView) findViewById(R.id.see_password_iv);
        see_password_iv.setOnClickListener(this);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_regist;
    }

    @Override
    public PRegister newP() {
        return new PRegister();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case  R.id.getiden_code_tv://获取验证码
                String phoneNum=mobile_num_et.getText().toString();
                if (StringUtil.isEmpty(phoneNum)){
                    ToastUtil.Toast(getString(R.string.phone_num_not_empty),this);
                } else if(!StringUtil.isPhone(phoneNum)){
                    ToastUtil.Toast(getString(R.string.phone_num_format_incorrect),this);
                } else {
                    vphonenum = phoneNum;
                    getP().getIdenCode(phoneNum);
                    timer.start();
                    getiden_code_tv.setEnabled(false);
                    getiden_code_tv.setBackgroundResource(R.mipmap.yujiaojuxingg);
                }
                break;
            case R.id.see_password_iv://查看密码
                if (!seepass){
                    seepass=true;
                    see_password_iv.setImageResource(R.mipmap.dakai);
                    password_et.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                }else{
                    seepass=false;
                    see_password_iv.setImageResource(R.mipmap.guanbi);
                    password_et.setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_PASSWORD);
                }
                break;
            case R.id.register://注册
                String mpPhone=mobile_num_et.getText().toString();
                String mpPassword=password_et.getText().toString();
                String cd= iden_code_et.getText().toString();
                if(StringUtil.isEmpty(mpPhone)||StringUtil.isEmpty(mpPassword)){
                    ToastUtil.Toast(getString(R.string.account_or_psd_not_empty),this);
                }else if(!StringUtil.isPhone(mpPhone)){
                    ToastUtil.Toast(getString(R.string.phone_num_format_incorrect),this);
                }else if(mpPassword.length()<6||mpPassword.length()>16||StringUtil.isEmpty(mpPassword)){
                    ToastUtil.Toast(getString(R.string.psd_length_six_sixteen),this);
                }else if(!mpPhone.equals(vphonenum)){
                    ToastUtil.Toast(getString(R.string.please_get_code),this);
                }else if("".equals(code)||!cd.equals(code)){
                    ToastUtil.Toast(getString(R.string.code_incorrect),this);
                }else{
                    Intent intent =new Intent(context,CertificationActivity.class);
                    intent.putExtra(Constant.INTENT_PROJECT_KEY,mpPhone + "," + mpPassword);
                    intent.putExtra(Constant.INTENT_INT_KEY,-1);
                    startActivityForResult(intent,Constant.GUEST_ROOM_THING_REQUEST_CODE);
                }
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == Constant.GUEST_ROOM_THING_REQUEST_CODE && resultCode == RESULT_OK){
            finish();
        }
    }

    @Override
    public void finish() {
        super.finish();
        timer.cancel();
    }

    /**
     * 设置code
     * @param model 服务返回的验证码，用于本地判断是否一致
     */
    public void setIden_code(InfoModel model){
        code = model.getInfo();
    }
    public void showError(NetError error){
        getvDelegate().toastShort(error.getMessage());
    }
}
